package com;

import com.sun.org.apache.xpath.internal.objects.XNumber;

import javax.sound.sampled.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class BGM {
    private Clip currentClip = null;

    public void stopMusic() {
        if (currentClip != null) {
            currentClip.stop();
            currentClip.close();  //释放资源
            currentClip = null;
        }
    }
    public void loadMusic1(int number){
        stopMusic();//关闭上一个音乐
        try {
            Clip bgm= AudioSystem.getClip();//获取一首音乐
            System.out.println();
            File is = new File("sound/bg"+ number+".wav");
            AudioInputStream ais=AudioSystem.getAudioInputStream(is);
            bgm.open(ais);
            bgm.loop(Clip.LOOP_CONTINUOUSLY);
            currentClip=bgm;
        } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
    }

}
